package com.yanqu.road.server.cmd.activity.zhenhuan;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ZhenHuanProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.zhenhuan.CrossZhenHuanMgr;
import com.yanqu.road.server.manager.activity.zhenhuan.CrossZhenHuanThirdProcessActivity;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = CrossProtocol.C_CROSS_ZHENHUAN_THIRD_PROCESS_JOIN_BANQUET, desc = "加入宴会")
public class CrossZhenHuanThirdProcessJoinBanquetCmd extends CrossCommand {

    private int activityId;

    private long targetUserId;

    private int banquetId;

    private PlayerProto.PlayerBaseTempMsg playerBaseData;

    private long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ZhenHuanProto.CrossZhenHuanThirdProcessJoinBanquetReqMsg reqMsg = ZhenHuanProto.CrossZhenHuanThirdProcessJoinBanquetReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        targetUserId = reqMsg.getTargetUserId();
        banquetId = reqMsg.getBanquetId();
        playerBaseData = reqMsg.getPlayerBaseData();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossZhenHuanThirdProcessActivity activity = CrossZhenHuanMgr.getThirdProcessActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_ACTIVITY_NOT_FOUND;
            return;
        }
        // 更新一下用户数据
        UserBaseInfo userBaseInfo = PlayerBasePb.parseToUserBaseInfo(playerBaseData);
        CrossUserMgr.updateUserBaseInfo(userId, userBaseInfo);

        // 加入宴会
        if (banquetId == 0) {
            ret = activity.joinBanquetOneKeyByCmd(serverId, userId, targetUserId);
        } else {
            ret = activity.joinBanquetByCmd(serverId, userId, targetUserId, banquetId);
        }
    }

    @Override
    public void sendData() {
        if (ret != 0) {
            ZhenHuanProto.ZhenHuanThirdProcessJoinBanquetRespMsg.Builder respMsg = ZhenHuanProto.ZhenHuanThirdProcessJoinBanquetRespMsg.newBuilder();
            respMsg.setRet(0);
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(Protocol.U_ZHENHUAN_THIRD_PROCESS_JOIN_BANQUET, respMsg));
        }
    }
}
